package com.store.serviceimpl;

import com.store.mapper.ProductMapper;
import com.store.pojo.Category;
import com.store.pojo.Product;
import com.store.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;

    @Override
    public List<Product> findAllProductInfo() {
        List<Product> productList = productMapper.findAllProductInfo();
        return productList;
    }

    @Override
    public String deleteByPId(int id, HttpServletResponse response) throws IOException {
        int rows = productMapper.deleteByPId(id);
        if(rows<=0){
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.println("<script>alert('删除失败');</script>");
            writer.close();
            return  "redirect:/product/list";
        }
        return  "redirect:/product/list";
    }

    @Override
    public int addProduct(Product product) {
        int row = productMapper.addProduct(product);
        return row;
    }

    @Override
    public Map<String, Object> doUpdateproPic(MultipartFile file, HttpSession session) {
        Map<String,Object> map = new HashMap<>();
        if (file == null) {
            map.put("state",201);
            map.put("msg","请选择上传的文件");
            return map;
        }
        //不为空，获取文件的大小
        if (file.getSize()>1024*1024*50){
            map.put("state",202);
            map.put("msg","上传的文件大于50M");
            return map;
        }
        //获取上传的文件名
        String filename = file.getOriginalFilename();
        //获取文件名的后缀 123.jsk . png
        int index = filename.lastIndexOf(".");
        String suffix = filename.substring(index);
        //用当前系统时间毫秒值
        long time = System.currentTimeMillis();
        String newFileName = time+suffix;//新文件名

        //要上传到哪个位置
        String path = session.getServletContext().getRealPath("/images/product");//路径可能有错

        //File 可表示文件和 目录，这里是目录
        File file1 = new File(path);
        if (!file1.exists()){
            //创建这个目录
            file1.mkdir();
        }
        String filePath=path+"/"+newFileName;//File.separator 相当于"/"和"\\"
        System. out. println("filePath::::"+filePath);

        //使用File对象描述文件的路径
        File file2=new File(filePath);
        try {
            //上传文件
            file.transferTo(file2);
            //更新数据库信息
            Integer uid = (Integer) session.getAttribute("uid");
            String username = (String) session.getAttribute("username");
            Integer pid = (Integer) session.getAttribute("pid");
            int row = productMapper.updateproPic("images/product/"+newFileName,pid);
            if (row<0){
                map.put("state",204);
                map.put("msg","上传失败1");
                return map;
            }
            map.put("state",200);
            map.put("msg","上传成功");
            //成功需要将图片的名字回显到页面
            map.put("path",newFileName);
            return map;

        } catch (IOException e) {
            e.printStackTrace();
            map.put("state",203);
            map.put("msg","上传失败2");
            return map;
        }
    }

    @Override
    public int getCountTotal() {
        int count = productMapper.getCount();
        return count;
    }


    @Override
    public List<Product> findProductByPage(Integer offSet, Integer pageSize) {
        List<Product> list = productMapper.selectProductsByPage(offSet,pageSize);
        return list;
    }

    /*@Override
    public Product findProductByPid(Integer pid) {
        if (pid==null){
            throw  new RuntimeException("id不能为空");
        }
        Product product = productMapper.findProductByPid(pid);
        return product;
    }*/

/*
    @Override
    public String toLimitPage(Integer currentPage, Model model) {
        currentPage = currentPage+=1;
        Integer pageSize=2;
        int totalSize = productMapper.getCount();
        Integer totalPage = totalSize%pageSize!=0? totalSize/pageSize+1:totalSize/pageSize;
        if(currentPage>=totalPage){
            currentPage=totalPage;
        }
        if(currentPage<=1){
            currentPage=1;
        }
        // Integer offSet=(currentPage-1)*pageSize;
        PageHelper.startPage(currentPage,pageSize);
        List<Product> productList = productMapper.findAllProductInfo();
        PageInfo<Product> productListInfo = new PageInfo<>(productList);
        List<Product> productListPage = productListInfo.getList();
        model.addAttribute("currentSize",productListPage.size());
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("productListPage",productListPage);
        model.addAttribute("currentPage",currentPage);
        return "adminpage/product-list";
    }

    @Override
    public String toUpPage(Integer currentPage, Model model) {
        currentPage = currentPage-=1;
        Integer pageSize=2;
        int totalSize = productMapper.getTotal();
        Integer totalPage = totalSize%pageSize!=0? totalSize/pageSize+1:totalSize/pageSize;
        if(currentPage<=1){
            currentPage=1;
        }
        //Integer offSet=(currentPage-1)*pageSize;
        PageHelper.startPage(currentPage,pageSize);
        List<Product> productList = productMapper.findAllProductInfo();
        PageInfo<Product> productListInfo = new PageInfo<>(productList);
        List<Product> productListPage = productListInfo.getList();
        model.addAttribute("currentSize",productListPage.size());
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("productListPage",productListPage);
        model.addAttribute("currentPage",currentPage);
        return "adminpage/product-list";
    }*/

}

